﻿using JT808Gateway.Data.Entity;
using SqlSugar;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace JT808Gateway.Data
{
    public class ForwardingServerBindingRepository : BaseRepository<ForwardingServerBinding>
    {
        public async Task<List<ForwardingServerBinding>> QueryPage(int pageIndx, int pageSize, RefAsync<int> total)
        {
            return await Db.Queryable<ForwardingServerBinding>()
                .Mapper(it => it.ForwardingServer, it => it.ServerId).ToPageListAsync(pageIndx, pageSize, total);
        }

        public async Task<List<ForwardingServerBinding>> QueryBindings(EnumNetType netType)
        {
            return await Db.Queryable<ForwardingServerBinding, ForwardingServer>((b, s) => new JoinQueryInfos(
                 JoinType.Left, b.ServerId == s.Id
                 )).Where((b, s) => b.Enabled == true && s.NetType == netType)
                .Mapper(it => it.ForwardingServer, it => it.ServerId).ToListAsync();
        }

        public async Task<List<ForwardingServerBinding>> QueryAll()
        {
            return await Db.Queryable<ForwardingServerBinding>()
                .Where(x => x.Enabled == true)
                .Mapper(it => it.ForwardingServer, it => it.ServerId).ToListAsync();
        }
    }
}